package com.exam.userms;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.exam.userms.dbmanage.helper.UserDataManager;

/**
 * 用户添加（注册）
 * Created by guopei on 2020/7/2
 */
public class Register extends AppCompatActivity {

    private EditText mAccount;                        //用户名编辑
    private EditText phone;                            //手机编辑
    private Button mSureButton;                       //确定按钮
    private Button mCancelButton;                     //取消按钮
    private UserDataManager mUserDataManager;         //用户数据管理类

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        mAccount = (EditText) findViewById(R.id.edit_name);
        phone = (EditText) findViewById(R.id.resetpwd_edit_pwd_new);

        mSureButton = (Button) findViewById(R.id.register_btn_sure);
        mCancelButton = (Button) findViewById(R.id.register_btn_cancel);
        mSureButton.setOnClickListener(m_register_Listener);      //注册界面两个按钮的监听事件
        mCancelButton.setOnClickListener(m_register_Listener);

        if (mUserDataManager == null) {
            mUserDataManager = new UserDataManager(this);
            mUserDataManager.openDataBase();                              //建立本地数据库
        }
    }

    View.OnClickListener m_register_Listener = new View.OnClickListener() {    //不同按钮按下的监听事件选择
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.register_btn_sure:                       //确认按钮的监听事件
                    register_check();
                    break;
                case R.id.register_btn_cancel:                     //取消按钮的监听事件,由注册界面返回登录界面
                    Intent intent_Register_to_Login = new Intent(Register.this, MainActivity.class);    //切换User Activity至Login Activity
                    startActivity(intent_Register_to_Login);
                    finish();
                    break;
            }
        }
    };

    public void register_check() {                                //确认按钮的监听事件
        if (isUserNameAndPhoneValid()) {
            String userName = mAccount.getText().toString().trim();
            String phone = this.phone.getText().toString().trim();

            UserData mUser = new UserData(userName, phone);
            mUserDataManager.openDataBase();
            long flag = mUserDataManager.insertUserData(mUser); //新建用户信息
            if (flag == -1) {
                Toast.makeText(this, getString(R.string.register_fail), Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, getString(R.string.register_success), Toast.LENGTH_SHORT).show();
                Intent intent_Register_to_Login = new Intent(Register.this, MainActivity.class);    //切换User Activity至Login Activity
                startActivity(intent_Register_to_Login);
                finish();
            }
        }
    }

    public boolean isUserNameAndPhoneValid() {
        if (mAccount.getText().toString().trim().equals("")) {
            Toast.makeText(this, getString(R.string.account_empty),
                    Toast.LENGTH_SHORT).show();
            return false;
        } else if (phone.getText().toString().trim().equals("")) {
            Toast.makeText(this, getString(R.string.phone_empty),
                    Toast.LENGTH_SHORT).show();
            return false;
        } else if (mAccount.getText().toString().trim().length() > 20) {
            Toast.makeText(this, getString(R.string.account_length_large),
                    Toast.LENGTH_SHORT).show();
            return false;
        } else if (phone.getText().toString().trim().length() > 20) {
            Toast.makeText(this, getString(R.string.phone_length_large),
                    Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }
}
